package com.demo.work_queues;

import com.demo.commons.MqCons;
import com.demo.commons.message.TestMessage;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author shenguangyang
 */
@Component
@RabbitListener(queues = MqCons.WORK_QUEUE)
public class WorkQueues01Listener {
    @RabbitHandler
    public void onMessage(TestMessage msg, Channel channel, Message message) {
        try {
            System.out.println(WorkQueues02Listener.class.getSimpleName() + ": " + msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
